package org.sensation.server.operation.response;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import org.sensation.server.operation.request.Command;

public class Response {
	private OutputStream out;
	private ObjectOutputStream oos;
	
	public Response(OutputStream out) throws IOException {
		super();
		this.out = out;
		this.oos = new ObjectOutputStream(out);
		
	}
	public void write(byte[] data) throws IOException{
		out.write(data);
		out.flush();
	}
	public void error(String message) throws IOException {
		String error = createErrorMessage(message);
		out.write(error.getBytes());
		out.flush();
	}
	public void complete(Command command) throws IOException {
		oos.writeObject(command);
		oos.flush();
	}

	private String createErrorMessage(String message) {
		return "{\"error\":\""+ message+"\"";
	}
}
